from flask import Flask, request, jsonify
from PlateDetectionService import PlateDetectionService
import os

app = Flask(__name__)
service = PlateDetectionService("../Yolov8_PlateDetect.pt")

# 确保临时文件夹存在
if not os.path.exists("../temp"):
    os.makedirs("../temp")


@app.route('/detect', methods=['POST'])
def detect_plate():
    if 'file' not in request.files:
        return jsonify({"error": "No file part"}), 400

    file = request.files['file']
    if file.filename == '':
        return jsonify({"error": "No selected file"}), 400

    # 保存文件到临时路径
    file_path = os.path.join("../temp", file.filename)
    file.save(file_path)

    # 调用服务处理图像
    results = service.process_image(file_path)

    # 删除临时文件
    os.remove(file_path)

    return jsonify(results)


if __name__ == '__main__':
    app.run(debug=True)
